package edu.hrbeu.crc.controller;

import edu.hrbeu.crc.pojo.dto.ShoppingListDTO;
import edu.hrbeu.crc.pojo.entity.ShoppingList;
import edu.hrbeu.crc.pojo.query.ShoppingListQuery;
import edu.hrbeu.crc.pojo.result.PageResult;
import edu.hrbeu.crc.pojo.result.Result;
import edu.hrbeu.crc.pojo.vo.ShoppingListPageVO;
import edu.hrbeu.crc.pojo.vo.ShoppingListVO;
import edu.hrbeu.crc.service.ShoppingListService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/shoppingList")
public class ShoppingListController {

    private final ShoppingListService shoppingListService;

    /**
     * 创建清单
     * @param shoppingListDTO
     * @return
     */
    @PostMapping
    public Result add(@RequestBody ShoppingListDTO shoppingListDTO) {
        log.info("创建清单：{}", shoppingListDTO);

        shoppingListService.add(shoppingListDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param shoppingListQuery
     * @return
     */
    @GetMapping("/list")
    public Result<PageResult<ShoppingListPageVO>> list(ShoppingListQuery shoppingListQuery) {

        PageResult<ShoppingListPageVO> p = shoppingListService.queryShoppingList(shoppingListQuery);
        return Result.success(p);
    }

    /**
     * 根据id查询清单
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<ShoppingListVO> getById(@PathVariable Integer id) {
        log.info("查询清单：{}",id);

        ShoppingListVO res = shoppingListService.get(id);
        return Result.success(res);
    }

    /**
     * 修改清单
     * @param shoppingListDTO
     * @return
     */
    @PutMapping
    public Result update(@RequestBody ShoppingListDTO shoppingListDTO) {
        log.info("修改清单：{}",shoppingListDTO);

        shoppingListService.updateShoppingList(shoppingListDTO);
        return Result.success();
    }

    /**
     * 批量删除清单
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("删除清单：{}",ids);

        shoppingListService.delete(ids);
        return Result.success();
    }

    /**
     * 更改清单状态
     * @param id
     * @return
     */
    @PostMapping("/status")
    public Result upStatus(@RequestParam Integer id) {
        log.info("更改清单为已完成：{}",id);

        shoppingListService.upStatus(id);
        return Result.success();
    }
}
